import React, {PureComponent} from 'react';
import {
    PAGE,
    TABLE,
    DATETIME_FORMAT,
    PAGINATION_OPTIONS,
    COMMON_TABLE_EMPTY_TEXT,
    COMMON_TABLE_QUERY_FAIL_TEXT,
} from '../constants';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import {conventSorter} from './utils';
import {timelinessType, orderUnmetReasonType} from '../Enum';
import {formatDateTime, formatAmount, formatPercent, conventEnumValueToString} from '../utils';
export class TablePanel extends PureComponent {
    state = {
        loading: false,
    };
    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({...conventSorter(sorter),
                pageIndex: PAGE.index});
    };
    onSelectedChange = (rkeys, keys) => {
        this.setState({
            selectedRowKeys: rkeys,
            selectedRows: keys
        });
    };
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.dealerCode',
                defaultMessage: 'ERP号'
            }),
            dataIndex: 'dealerCode',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.code',
                defaultMessage: '订单号'
            }),
            dataIndex: 'code',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partManager',
                defaultMessage: '备件经理'
            }),
            dataIndex: 'partManager',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.marketDepartmentName',
                defaultMessage: '大区名称'
            }),
            dataIndex: 'marketDepartmentName',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.country',
                defaultMessage: '经销商'
            }),
            dataIndex: 'country',
            sorter: true
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partOrderTypeName',
                defaultMessage: '订单类型'
            }),
            dataIndex: 'partOrderTypeName'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.submitDate',
                defaultMessage: '订单提报日期'
            }),
            dataIndex: 'submitDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.confirmTime',
                defaultMessage: 'PI确认日期'
            }),
            dataIndex: 'confirmTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.itemCount',
                defaultMessage: 'PI品种'
            }),
            dataIndex: 'itemCount'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.fobTotalAmount',
                defaultMessage: 'FOB金额'
            }),
            dataIndex: 'fobTotalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.quantity',
                defaultMessage: 'PI数量'
            }),
            dataIndex: 'quantity'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.creditAuditedTime',
                defaultMessage: '付款日期'
            }),
            dataIndex: 'creditAuditedTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.orderDuedate',
                defaultMessage: '订单到期日'
            }),
            dataIndex: 'orderDuedate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.partInvoiceCode',
                defaultMessage: '发票号'
            }),
            dataIndex: 'partInvoiceCode'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.invoiceCreateTime',
                defaultMessage: '发票生成日期'
            }),
            dataIndex: 'invoiceCreateTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.shipmentDate',
                defaultMessage: '出运日期'
            }),
            dataIndex: 'shipmentDate',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockItem',
                defaultMessage: '备货品种'
            }),
            dataIndex: 'stockItem'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockMoney',
                defaultMessage: '备货金额'
            }),
            dataIndex: 'stockMoney',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockQuantity',
                defaultMessage: '备货数量'
            }),
            dataIndex: 'stockQuantity'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.itemCountInvoice',
                defaultMessage: '发运品种'
            }),
            dataIndex: 'itemCountInvoice'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalAmountInvoice',
                defaultMessage: '发运金额'
            }),
            dataIndex: 'totalAmountInvoice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalQuantityInvoice',
                defaultMessage: '发运数量'
            }),
            dataIndex: 'totalQuantityInvoice'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockItemRate',
                defaultMessage: '备货品种满足率'
            }),
            dataIndex: 'stockItemRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockMoneyRate',
                defaultMessage: '备货金额满足率'
            }),
            dataIndex: 'stockMoneyRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockQuantityRate',
                defaultMessage: '备货数量满足率'
            }),
            dataIndex: 'stockQuantityRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.itemCountInvoiceRate',
                defaultMessage: '发运品种满足率'
            }),
            dataIndex: 'itemCountInvoiceRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalAmountInvoiceRate',
                defaultMessage: '发运金额满足率'
            }),
            dataIndex: 'totalAmountInvoiceRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalQuantityInvoiceRate',
                defaultMessage: '发运数量满足率'
            }),
            dataIndex: 'totalQuantityInvoiceRate',
            render: text => formatPercent(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.totalShipmentDay',
                defaultMessage: '总出运天数'
            }),
            dataIndex: 'totalShipmentDay'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.timelinessType',
                defaultMessage: '及时性'
            }),
            dataIndex: 'timelinessType',
            render: text => conventEnumValueToString(timelinessType, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.orderUnmetReasonType',
                defaultMessage: '不及时分类'
            }),
            dataIndex: 'orderUnmetReasonType',
            render: text => conventEnumValueToString(orderUnmetReasonType, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.abnormalExplainRate',
                defaultMessage: '异常说明-满足率'
            }),
            dataIndex: 'abnormalExplainRate'
        }, {
            title: formatMessage({
                id: 'tablePanel.column.abnormalExplainTimeRate',
                defaultMessage: '异常说明-及时率'
            }),
            dataIndex: 'abnormalExplainTimeRate'
        }];
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <Card>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}

TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
};
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
});

const mapDispatchToProps = dispatch => ({
    onPageSizeChange: (current, pageSize) =>
        dispatch(
            tableSearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableSearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableSearch(option))
});
import {connect} from 'react-redux';
import {injectIntl} from './intl';
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
